Component({
    data: {
      currentYear: new Date().getFullYear(), // 当前年份
      currentMonth: new Date().getMonth() + 1, // 当前月份
      calendarData: {
        prevDays: [],   // 上月的数据
        currentDays: [], // 当前月的数据
        nextDays: [],   // 下月的数据
      },
      translateY: 0, // 用于监控移动方向和位置
      calendarHeight: 0, // 动态计算日历高度
    },
  
    lifetimes: {
      attached() {
        // 初始化日历
        this.setCalendar();
      },
    },
  
    methods: {
      // 初始化渲染日历
      setCalendar() {
        const { currentYear, currentMonth } = this.data;
  
        // 分别生成当前月、上月、下月的数据
        this.setData({
          calendarData: {
            currentDays: this.generateMonthDays(currentYear, currentMonth),
            prevDays: this.generateMonthDays(
              currentMonth === 1 ? currentYear - 1 : currentYear,
              currentMonth === 1 ? 12 : currentMonth - 1
            ),
            nextDays: this.generateMonthDays(
              currentMonth === 12 ? currentYear + 1 : currentYear,
              currentMonth === 12 ? 1 : currentMonth + 1
            ),
          },
          translateY: 0, // 初始化滑动偏移量
        });
      },
  
      // 根据年份和月份生成天数
      generateMonthDays(year, month) {
        const days = [];
        const firstDay = new Date(year, month - 1, 1);
        const lastDay = new Date(year, month, 0);
  
        // 补齐上个月尾部
        const firstWeekDay = firstDay.getDay();
        for (let i = firstWeekDay - 1; i >= 0; i--) {
          days.push({
            day: new Date(year, month - 2, lastDay.getDate() - i).getDate(),
            isSelected: false,
          });
        }
  
        // 当前月
        for (let i = 1; i <= lastDay.getDate(); i++) {
          days.push({
            day: i,
            isSelected: false,
          });
        }
  
        // 补齐下个月前置
        const remainingDays = 7 * Math.ceil(days.length / 7) - days.length; // 维持完整的格子数
        for (let i = 1; i <= remainingDays; i++) {
          days.push({
            day: i,
            isSelected: false,
          });
        }
  
        return days;
      },
  
      // 滑动事件（结束后切换内容）
      onScrollEnd(e) {
        const { translateY, calendarHeight } = this.data;
  
        // 判断滑动方向
        if (translateY > calendarHeight / 2) {
          // 上月
          this.switchToPrevMonth();
        } else if (translateY < -calendarHeight / 2) {
          // 下月
          this.switchToNextMonth();
        } else {
          // 恢复到当前月中心
          this.setData({ translateY: 0 });
        }
      },
  
      // 切换到上个月
      switchToPrevMonth() {
        let { currentYear, currentMonth } = this.data;
  
        if (currentMonth === 1) {
          currentYear--;
          currentMonth = 12;
        } else {
          currentMonth--;
        }
  
        this.setData(
          {
            currentYear,
            currentMonth,
            translateY: 0, // 重置位置
          },
          this.setCalendar // 更新日历数据
        );
      },
  
      // 切换到下个月
      switchToNextMonth() {
        let { currentYear, currentMonth } = this.data;
  
        if (currentMonth === 12) {
          currentYear++;
          currentMonth = 1;
        } else {
          currentMonth++;
        }
  
        this.setData(
          {
            currentYear,
            currentMonth,
            translateY: 0, // 重置位置
          },
          this.setCalendar // 更新日历数据
        );
      },
  
      // 滑动动画完成事件
      onScrollAnimationFinish(e) {
        this.setData({ translateY: e.detail.y });
      },
    },
  });
  